package com.mdxx.xczd.chat.websocket.json;

import android.util.JsonReader;
import android.util.JsonToken;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class ParseJson {
	private String type;
	private boolean success;
	private String userid;
	private String username;
	private String rtspurl;
	private String selftimestart;          //服务器返回的用户始点击录音时
	private ConnectSuccess userSelfInfo; // 记录本地用户连接成功后获得的自己的信,flag=1
	private UserList talkingUserList; // 记录服务端参加说话的有用户列,flag=2
	private BeginTalking beginTalking; // 记录其他讲话用户的信,flag=3
	private StopTalking stopTakling; // 记录其他结束讲话的用户的信息,flag=4
	private List<UserInfo> userlist = new ArrayList<UserInfo>();
	private int flag = 0; // 由flag标志此时正在解析的类�?,
	
	
	//新的抢线协议中的几个�?
	private String TYPE;
	private String RTSP;//RTSP路径
	private String AUTH;//用户权限
	private String USER;//用户

	
	

	public String getTYPE() {
		return TYPE;
	}

	public String getRTSP() {
		return RTSP;
	}

	public String getAUTH() {
		return AUTH;
	}

	public String getUSER() {
		return USER;
	}

	public String getType() {
		return type;
	}

	public boolean getSuccess() {
		return success;
	}

	public ConnectSuccess getUserSelfInfo() {
		return userSelfInfo;
	}

	public UserList getTalkingUserList() {
		return talkingUserList;
	}

	public BeginTalking getBeginTalking() {
		return beginTalking;
	}
	
	public String getSelftimestart() {
		return selftimestart;
	}

	public void setSelftimestart(String selftimestart) {
		this.selftimestart = selftimestart;
	}


	public StopTalking getStopTakling() {
		return stopTakling;
	}

	public List<UserInfo> getUserlist() {
		return userlist;
	}

	public int getFlag() {
		return flag;
	}
	

	public String getRtspurl() {
		return rtspurl;
	}

	public void setRtspurl(String rtspurl) {
		this.rtspurl = rtspurl;
	}

	public String jsonParse(InputStream in) {
		String result = "success";
		try {
			// JsonReader reader = new JsonReader(new StringReader(in));
			JsonReader reader = new JsonReader(new InputStreamReader(in,
					"UTF-8"));
			reader.beginObject();
			flag = 0;
			while (reader.hasNext()) {
				String tagName = reader.nextName();
				if (tagName.equals("type")) {
					type = reader.nextString();
				} else if (tagName.equals("success")) {
					success = reader.nextBoolean();
				}  else if (tagName.equals("data")) {
					username = null;
					String begin = null;
					String end = null;
					JsonToken datatype = reader.peek();
					if (datatype == JsonToken.BEGIN_OBJECT) {
						reader.beginObject();
						while (reader.hasNext()) {
							tagName = reader.nextName();
							if (tagName.equals("id")) {
								userid = String.valueOf(reader.nextInt());
							}else if (tagName.equals("starttype")) {
								selftimestart = reader.nextString();
							}else if (tagName.equals("rtspurl")) {
								rtspurl = reader.nextString();
							}  else if (tagName.equals("name")) {
								username = reader.nextString();
							} else if (tagName.equals("begin")) {
								begin = reader.nextString();
							} else if (tagName.equals("end")) {
								end = reader.nextString();
							}else if (tagName.equals("TYPE")) {
								TYPE = reader.nextString();
							}else if (tagName.equals("RTSP")) {
								RTSP = reader.nextString();
							}  else if (tagName.equals("AUTH")) {
								AUTH = reader.nextString();
							}else if (tagName.equals("USER")) {
								USER = reader.nextString();
							} 
							
						}
						reader.endObject();
						if (username == null) {
							flag = 3;
							beginTalking = new BeginTalking();
							UserInfo userinfo = new UserInfo();
							userinfo.setId(userid);
							beginTalking.setData(userinfo);
						} else if (username != null) {
							flag = 1;
							userSelfInfo = new ConnectSuccess();
							UserInfo userinfo = new UserInfo();
							userinfo.setId(userid);
							userinfo.setName(username);
							userSelfInfo.setData(userinfo);
						} else if (begin != null && end != null) {
							flag = 4;
							stopTakling = new StopTalking();
							StopUser stopuser = new StopUser();
							stopuser.setId(userid);
							stopuser.setBegin(begin);
							stopuser.setEnd(end);
							stopTakling.setData(stopuser);
						}
					} else if (datatype == JsonToken.BEGIN_ARRAY) {
						flag = 2;
						reader.beginArray();
						UserInfo userinfo = new UserInfo();
						while (reader.hasNext()) {
							reader.beginObject();
							while (reader.hasNext()) {
								tagName = reader.nextName();
								if (tagName.equals("id")) {
									userinfo.setId(String.valueOf(reader
											.nextInt()));
								} else if (tagName.equals("name")) {
									userinfo.setName(reader.nextString());
								} else {
									reader.nextString();
								}
							}
							reader.endObject();
							userlist.add(userinfo);
						}
						reader.endArray();
						talkingUserList = new UserList();
						talkingUserList.setData(userlist);
					}

				}

			}
			reader.endObject();
			reader.close();

			switch (flag) {
			case 1:
				userSelfInfo.setType(type);
				userSelfInfo.setSuccess(success);
				break;
			case 2:
				talkingUserList.setType(type);
				talkingUserList.setSuccess(success);
				break;
			case 3:
				beginTalking.setType(type);
				beginTalking.setSuccess(success);
				break;
			case 4:
				stopTakling.setType(type);
				stopTakling.setSuccess(success);
				break;
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			result = "解析出错";
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			result = "解析出错";
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public String toString() {
		return "ParseJson [userSelfInfo=" + userSelfInfo + ", talkingUserList="
				+ talkingUserList + ", beginTalking=" + beginTalking
				+ ", stopTakling=" + stopTakling + ", flag=" + flag + "]";
	}

}